package utils
{
	import flash.display.Graphics;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class SectorAnimation
	{
		private var m_graphics : Graphics;
		private var m_totalAngle : uint;
		private var m_totalTime : uint;
		private var m_duration : uint;
		private var m_angle : Number;
		private var m_timer : Timer;
		private static const DELAY : uint = 100;

		public function SectorAnimation()
		{
			m_timer = new Timer( DELAY );
			m_timer.addEventListener( TimerEvent.TIMER, onUpdateAnimation );
			m_timer.addEventListener( TimerEvent.TIMER_COMPLETE, onAnimationComplete );
		}

		private function onUpdateAnimation( evt : TimerEvent ) : void
		{
			this.m_graphics.clear();

			this.m_graphics.beginFill( 0xffff00 );
			SectorUtil.drawSector( this.m_graphics, 100, 100, 100, m_angle * m_timer.currentCount );
			this.m_graphics.endFill();
		}

		private function onAnimationComplete( evt : TimerEvent ) : void
		{
			this.m_graphics.clear();
		}

		public function startAnimation( graphics : Graphics, totalAngle : uint, totalTime : uint, durationTime : uint ) : void
		{
			this.m_graphics = graphics;
			this.m_totalAngle = totalAngle;
			this.m_totalTime = totalTime;

			if ( durationTime >= m_totalTime )
				this.m_duration = m_totalTime;
			else
				this.m_duration = durationTime;

			m_timer.reset();
			m_timer.repeatCount = m_duration / DELAY;
			m_angle = ( m_totalAngle * this.m_duration / m_totalTime ) / m_timer.repeatCount;
			m_timer.start();
		}
	}
}
